//
//  LYDiscussTimeView.m
//  LyCompassApp
//
//  Created by QD on 16/8/3.
//  Copyright © 2016年 览益信息科技. All rights reserved.
//

#import "LYDiscussTimeView.h"
#import "LYCommentModel.h"
#import "LYUtil.h"
#import "UIView+Frame.h"

static NSInteger kContentFontSize  = 12.0f;
static NSInteger kDecreaseWidth    = 100.0f;
static NSInteger kSelfOriginHeight = 35.0f;
static NSInteger kIncreaseWidth    = 20.0f;

@interface LYDiscussTimeView ()
@property (nonatomic,strong) NSString *showTime;
@property (nonatomic,strong) NSString *timeString;
@property (nonatomic,strong) NSDateFormatter *dateFormatter;
@end


@implementation LYDiscussTimeView

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.timeLab.layer.cornerRadius = 3;
    self.timeLab.layer.masksToBounds = YES;
}

+ (instancetype)viewWithXib
{
    LYDiscussTimeView *view = [[[UINib nibWithNibName:@"LYDiscussTimeView" bundle:nil] instantiateWithOwner:self options:nil] lastObject];
    return view;
}

+ (CGFloat)returnHeightWithModel:(LYCommentModel *)model
{
    return kSelfOriginHeight;
}

- (void)setCommentModel:(LYCommentModel *)commentModel
{
    _commentModel = commentModel;
    
    if (![LYUtil isNilOrEmpty:self.commentModel.show.ctime]) {
        
        self.timeLab.width = [LYUtil sizeWithText:[self _returnTimeStringWithModel:self.commentModel] font:[UIFont systemFontOfSize:kContentFontSize] withWidth:SCREEN_WIDTH - kDecreaseWidth].width + kIncreaseWidth;
        self.timeLab.center = CGPointMake(self.center.x, self.center.y + 3) ;
        self.timeLab.text = [self _returnTimeStringWithModel:self.commentModel];
    }
}

#pragma mark ----- private methods
- (NSString *)_returnTimeStringWithModel:(LYCommentModel *)commentModel
{
    NSString *timeString = @"";
    NSString *date = [LYUtil compareDate:[self.dateFormatter dateFromString:self.commentModel.show.ctime]];
    if (self.commentModel.show.ctime.length <= 16)  return nil;
    
    self.showTime = [self.commentModel.show.ctime substringWithRange:NSMakeRange(11, 5)];
    if ([date isEqualToString:@"今日"]) {
        
        timeString = [self.commentModel.show.ctime substringWithRange:NSMakeRange(11, 5)];
    }else if ([date isEqualToString:@"昨日"]){
        
        timeString = [NSString stringWithFormat:@"%@ %@",@"昨天",self.showTime];
    }else if ([date isEqualToString:@"前日"]){
        
        timeString = [NSString stringWithFormat:@"%@ %@",@"前天",self.showTime];
    }else{
        //超过前天的历史记录显示月日
        timeString = [NSString stringWithFormat:@"%@ %@",[self.commentModel.show.ctime substringWithRange:NSMakeRange(5, 5)],self.showTime];
    }
    return timeString;
}

#pragma mark ----- getters and setters
-(NSDateFormatter *)dateFormatter{
    
    if (_dateFormatter == nil) {
        
        _dateFormatter = [[NSDateFormatter alloc] init];
        [_dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    }
    
    return _dateFormatter;
}



@end
